// packageA/pages/seckill-list/index.js
const api = require('../../../utils/api-tp.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    status: 0, // 状态 0:可以抢 1:标识已抢光
    seckillList: [], //秒杀列表
    pageIndex: 0,
    pageSize: 10,
    hasMore: true,
    current_id: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    api.isLogin(this);
    this.getSeckilTimeZone();
  },

  searchById(e) {
    let id = e.currentTarget.dataset.id;
    if (id == this.data.current_id) return;
    this.setData({
      current_id: id,
      pageIndex: 0,
      hasMore: true,
      seckillList: []
    })
    this.getSeckillList();
  },

  /**
   * 获取秒杀时间区
   */
  getSeckilTimeZone() {
    api.post({
      url: '/wxsmall/seckill/getSearchTime',
      data: {
        token: this.data.token
      },
      success: (res) => {
        // console.log(res)
        let ret = res.data;
        // 设置默认
        let default_id = '';
        if (ret.length > 0) {
          for (let i in ret) {
            if (ret[i].status == 1) {
              default_id = ret[i].id
            }
          }
          this.setData({
            timeZoneList: ret || [],
            current_id: default_id
          })

          if (default_id) { // 如果存在则去请求 否则不请求
            this.getSeckillList();
          }
        }
      }
    })
  },

  goDetail: function(e) {
    let temp = e.currentTarget.dataset;
    let flag = temp.flag;
    let id = temp.id
    let status = temp.status;
    if (status == 0 || status == 2) return;
    wx.navigateTo({
      url: '/pages/product-detail/index?id=' + id,
    })

  },

  /**
   * 获取秒杀列表 分页
   */
  getSeckillList: function() {
    let rep = this.data;
    if (!rep.hasMore) return;
    wx.showLoading({
      title: '加载中...',
    })
    api.post({
      url: '/wxsmall/Seckill/getList',
      data: {
        page: ++rep.pageIndex,
        row: rep.pageSize,
        search_time_id: rep.current_id
      },
      success: (res) => {
        // console.log(res)
        let ret = res.data
        if (ret.length < rep.pageSize) {
          this.setData({
            hasMore: false
          })
        }
        let newList = rep.seckillList.concat(ret);
        this.setData({
          seckillList: newList || []
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.getSeckillList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})